package com.wzy.vx.service.impl;

import com.wzy.vx.DTO.OrderDTO;
import com.wzy.vx.enums.ResuletEnum;
import com.wzy.vx.exception.SellException;
import com.wzy.vx.service.BuyerService;
import com.wzy.vx.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.service.impl
 * @ClassName: BuyerServiceImpl
 * @Author: Administrator
 * @Description: ${description}
 * @Date: 2019/3/14 13:09
 * @Version: 1.0
 */
@Service
public class BuyerServiceImpl implements BuyerService {
    @Autowired
    private OrderService orderService;

    private final  static Logger log = LoggerFactory.getLogger(BuyerServiceImpl.class);

    @Override
    public OrderDTO findOrderOne(String openid, String orderId) {
        return checkOrderOwner(openid,orderId);
    }

    @Override
    public OrderDTO cannelOrder(String openid, String orderId) {
      OrderDTO result = checkOrderOwner(openid,orderId);
      if (result == null){
          log.error("[取消订单]查不到该订单,orderId={}",orderId);
          throw new SellException(ResuletEnum.ORDER_NOT_EXIST);
      }
        return orderService.cannel(result);
    }

    private OrderDTO checkOrderOwner(String openid, String orderId) {
        OrderDTO orderDTO = orderService.findOrder(orderId);
        if (orderDTO == null){
            return null;
        }
        if (!orderDTO.getBuyerOpenid().equals(openid)){
            log.error("[买家查询订单]订单openid不一致,openid={},orderId={}",openid,orderId);
            throw new SellException(ResuletEnum.ORDER_OWNER_ERROR);
        }
        return orderDTO;
}}
